package com.infra.micro.quartz.domain.repository;

import com.infra.common.util.ConvertUtils;
import com.infra.micro.quartz.domain.entity.QuartzJobLogExample;
import com.infra.micro.quartz.domain.entity.QuartzJobLogPO;
import com.infra.micro.quartz.domain.mapper.QuartzJobLogMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class QuartzJobLogRepository {

    @Resource
    private QuartzJobLogMapper quartzJobLogMapper;

    public boolean insert(QuartzJobLogPO record) {
        return quartzJobLogMapper.insertSelective(record) > 0;
    }

    public boolean update(QuartzJobLogPO record) {
        return quartzJobLogMapper.updateByPrimaryKeyWithBLOBs(record) > 0;
    }

    public QuartzJobLogPO queryById(Long logId) {
        return quartzJobLogMapper.selectByPrimaryKey(logId);
    }

    public int deleteById(Long logId) {
        return quartzJobLogMapper.deleteByPrimaryKey(logId);
    }

    public int batchDeleteByJobIds(List<Long> logIds) {
        QuartzJobLogExample example = new QuartzJobLogExample();
        example.createCriteria().andJobIdIn(logIds);
        return quartzJobLogMapper.deleteByExample(example);
    }

    public int batchDeleteByLogIds(List<Long> logIds) {
        QuartzJobLogExample example = new QuartzJobLogExample();
        example.createCriteria().andLogIdIn(logIds);
        return quartzJobLogMapper.deleteByExample(example);
    }

    public List<QuartzJobLogPO> queryListByJobId(Long jobId) {
        Map<String, Object> params = new HashMap<>();
        params.put("jobId", jobId);
        return queryListByConf(params);
    }

    public List<QuartzJobLogPO> queryListByConf(Map<String, Object> params) {
        QuartzJobLogExample example = new QuartzJobLogExample();
        QuartzJobLogExample.Criteria criteria = example.createCriteria();
        if (CollectionUtils.isEmpty(params)) {
            return quartzJobLogMapper.selectByExample(example);
        }

        String beginDateStr = ConvertUtils.parseString(params.get("beginDate"));
        if (StringUtils.isNotBlank(beginDateStr)) {
            Date beginDate = ConvertUtils.parseDate(beginDateStr);
            if (beginDate != null) {
                criteria.andCreateTimeGreaterThanOrEqualTo(beginDate);
            }
        }
        String endDateStr = ConvertUtils.parseString(params.get("endDate"));
        if (StringUtils.isNotBlank(beginDateStr)) {
            Date endDate = ConvertUtils.parseDate(endDateStr);
            if (endDate != null) {
                criteria.andCreateTimeLessThan(DateUtils.addDays(endDate, 1));
            }
        }

        Long jobId = ConvertUtils.parseLong(params.get("jobId"));
        if (jobId != null && jobId > 0) {
            criteria.andJobIdEqualTo(jobId);
        }
        String beanName = ConvertUtils.parseString(params.get("beanName"));
        if (StringUtils.isNotBlank(beanName)) {
            criteria.andBeanNameLike(beanName + "%");
        }
        Boolean succeed = ConvertUtils.parseBoolean(params.get("isSucceed"));
        if (succeed != null) {
            criteria.andIsSucceedEqualTo(succeed);
        }
        String hostName = ConvertUtils.parseString(params.get("hostName"));
        if (StringUtils.isNotBlank(hostName)) {
            criteria.andHostNameEqualTo(beanName);
        }

        return quartzJobLogMapper.selectByExample(example);
    }

}
